<template>
  <div class="emp">
    <el-card>
      <div slot="header" class="header">员工列表</div>
      <div class="card-body">
        <!-- 条件查询 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          :model="searchData"
          ref="searchFormData"
        >
          <el-form-item label="员工姓名">
            <el-input
              placeholder="员工姓名"
              v-model="searchData.name"
            ></el-input>
          </el-form-item>
          <el-form-item label="员工账号">
            <el-input
              placeholder="员工账号"
              v-model="searchData.account"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系方式">
            <el-input
              placeholder="联系方式"
              v-model="searchData.phone"
            ></el-input>
          </el-form-item>
          <el-form-item label="角色">
            <el-select placeholder="角色" v-model="searchData.role">
              <el-option label="普通员工" value="employee"></el-option>
              <el-option label="收银员" value="sales"></el-option>
              <el-option label="管理员" value="admin"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
        <!-- 员工列表的表格 -->
        <el-table
          :data="empDatas"
          stripe
          highlight-current-row
          :style="{ width: tableWidth + 'px' }"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="name" label="姓名"> </el-table-column>
          <el-table-column prop="account" label="账号"> </el-table-column>
          <el-table-column prop="phone" label="联系方式"> </el-table-column>
          <el-table-column prop="sex" label="性别"> </el-table-column>
          <el-table-column prop="role" label="角色" :formatter="formatRole">
          </el-table-column>
          <el-table-column prop="status" label="状态" :formatter="formatStatus">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页条 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 修改弹窗 -->
    <el-dialog title="修改员工数据" :visible.sync="dialogFormVisible">
      <el-form :model="updateForm" label-width="80px">
        <el-input v-model="updateForm.id" type="hidden"></el-input>
        <el-form-item label="姓名">
          <el-input v-model="updateForm.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="updateForm.account" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="updateForm.phone" placeholder="电话"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select placeholder="角色" v-model="updateForm.role">
            <el-option label="普通员工" value="employee"></el-option>
            <el-option label="收银员" value="sales"></el-option>
            <el-option label="管理员" value="admin"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select placeholder="状态" v-model="updateForm.status">
            <el-option label="激活" value="active"></el-option>
            <el-option label="禁用" value="inactive"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateEmpData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getEmps, deleteEmpById, addEmp } from "@/api/user.js";
import { mapState } from "vuex";
export default {
  data() {
    return {
      // 修改员工弹窗隐藏和显示的标记
      dialogFormVisible: false,
      // 修改员工数据
      updateForm: {
        id: "",
        name: "",
        account: "",
        phone: "",
        role: "",
        status: "",
      },
      // 分页数据
      currentPage: 1,
      pageSize: 10,
      total: 31,

      // 员工数据
      empDatas: [],
      // 条件查询数据
      searchData: {
        name: "",
        account: "",
        phone: "",
        role: "",
      },

      // 表格的宽度
      tableWidth: 0,
    };
  },
  methods: {
    // 分页相关的函数
    handleCurrentChange(page) {
      this.currentPage = page;
      this.loadEmps();
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.loadEmps();
    },

    // 表格中编辑按钮对应的函数
    handleEdit(row) {
      this.updateForm = row;
      this.dialogFormVisible = true;
    },
    // 表格中删除按钮对应的函数
    handleDelete(row) {
      if (row.role == "admin") {
        this.$message({
          message: "管理员账号无法删除",
          type: "error",
        });
        return;
      }
      deleteEmpById(row.id).then((resp) => {
        let respData = resp.data;
        if ((respData.code = 20000)) {
          //   重新加载表格数据
          this.loadEmps();
        }
      });
    },

    //  添加查询按钮调用的方法
    onSubmit() {
      console.log(this.searchData);
      this.loadEmps();
    },

    // 查询员工数据
    loadEmps() {
      getEmps(this.currentPage,this.pageSize,this.searchData).then((resp) => {
        console.log(resp);
        let respData = resp.data;
        if (respData.code == 20000) {
          //数据列表
          this.empDatas = respData.data.rows;
          //总页码数
          this.total = respData.data.total;
        }
      });
    },

    // 角色的显示内容格式化
    formatRole(row, column, cellValue, index) {
      if (cellValue === "admin") {
        return "管理员";
      } else if (cellValue === "employee") {
        return "普通员工";
      } else if (cellValue === "sales") {
        return "收银员";
      }
    },

    // 账号状态处理
    formatStatus(row, column, cellValue, index) {
      if (cellValue === "active") {
        return "启用";
      } else if (cellValue === "inactive") {
        return "禁用";
      }
    },

    // 修改员工数据
    updateEmpData() {
      console.log(this.updateForm);
      addEmp(this.updateForm).then((resp) => {
        let respData = resp.data;
        if (respData.code == 20000) {
          // 修改的表单中的数据清掉
          this.updateForm = {};
          // 重新加载表格数据
          this.loadEmps();
          // 隐藏修改的对话框
          this.dialogFormVisible = false;
        }
      });
    },
    // 计算表格需要的实际宽度
    resizeTableWidth() {
      this.tableWidth = screen.availWidth - 300;
    },
  },
  // 计算属性
  computed: {
    ...mapState(["isCollapse"]),
  },
  // watch监控变化
  watch: {
    isCollapse() {
      this.resizeTableWidth();
    },
  },
  // watch监控变化
  watch: {
    isCollapse() {
      this.resizeTableWidth();
    },
  },
  // 利用声明周期函数，调用加载员工数据的方法
  created() {
    this.loadEmps();
    this.resizeTableWidth();
  },
};
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 20px;
}
/deep/ .el-dialog {
  width: 600px;
  .el-input {
    width: 440px;
  }
}
</style>
